package com.by.service.impl;

import com.by.bean.Freight;
import com.by.bean.User;
import com.by.bean.UserType;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.ex.BusinessException;
import com.by.common.utils.Assert;
import com.by.mapper.FreightMapper;
import com.by.mapper.UserMapper;
import com.by.query.FreightQuery;
import com.by.service.FreightService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service
public class FreightServiceImpl extends BaseServiceImpl<Freight, FreightMapper> implements FreightService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int save(Freight entity) {
        //检查账号唯一性
        int c = userMapper.queryUserByUserName(entity.getAccount());
        if(c>0)
            throw new BusinessException("该账号已存在");
        //保存用户
        User user = new User(entity.getAccount(), entity.getPassword(), UserType.FORWARDER_USER);
        int rs=userMapper.save(user);
        Assert.isZero(rs,"货代用户保存失败");
        entity.setId(user.getId());
        return super.save(entity);
    }
    @Override
    public int delete(Serializable id) {
        //删除用户
        int rs = userMapper.delete(id);
        Assert.isZero(rs,"删除货代信息失败");
        return super.delete(id);
    }
    public PageBean query(FreightQuery query){
        //分页查询
        PageHelper.startPage(query.getPage(),query.getPageSize());
        List list=baseMapper.query(query);
        PageInfo info=new PageInfo(list);
        return new PageBean(query.getPage(), query.getPageSize(), info.getTotal(), list);
    }
}
